<?php
    
    //去除html标签及空格换行
    function delete_html($str) {
        $str = strip_tags($str);
        $str = str_replace(array("\t", "\r\n", "\r", "\n", " "), "", $str);
        return $str;
    }
    
    //utf8编码字符串截取函数
    function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') {
        if ($code == 'UTF-8') {
            $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
            preg_match_all($pa, $string, $t_string);

            if (count($t_string[0]) - $start > $sublen)
                return join('', array_slice($t_string[0], $start, $sublen)) . "...";
            return join('', array_slice($t_string[0], $start, $sublen));
        } else {
            $start = $start * 2;
            $sublen = $sublen * 2;
            $strlen = strlen($string);
            $tmpstr = '';
            for ($i = 0; $i < $strlen; $i++) {
                if ($i >= $start && $i < ($start + $sublen)) {
                    if (ord(substr($string, $i, 1)) > 129)
                        $tmpstr .= substr($string, $i, 2);
                    else
                        $tmpstr .= substr($string, $i, 1);
                }
                if (ord(substr($string, $i, 1)) > 129)
                    $i++;
            }
            if (strlen($tmpstr) < $strlen)
                $tmpstr .= "...";
            return $tmpstr;
        }
    }
    
    //curl
    function curl_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * 短信接口二，触发类模板短信接口，可以设置动态参数变量。适合：验证码、订单短信等。
     * 1、传入目标手机号码，多个以“,”分隔，一次性调用最多100个号码，示例：139********,138********
     * 2、传入模板参数。短信模板内容示例：
     * 【微米网】您的验证码是：%P%，%P%分钟内有效。如非您本人操作，可忽略本消息。
     * 传入两个参数：p1：610912 p2：3
     * 最终发送内容：【微米网】您的验证码是：610912，3分钟内有效。如非您本人操作，可忽略本消息。
     */
    //注册时验证码函数
    function send_reg_msg($mobile,$msg_content){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://api.weimi.cc/2/sms/send.html");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, 'uid=GoHGupcRHpt5&pas=xf7zpy69&mob='.$mobile.'&cid=an8VIPQddbLA&p1='.$msg_content.'&type=json');
        $res = curl_exec($ch);
        curl_close($ch);
        
        $res = json_decode($res,true);;
        if($res['code'] === 0){
            return true;
        }else{
            return false;
        }
    }
    
    //随机函数
    function code_random($length = 6 , $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }
    
    function xml_to_array($xml){
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] = xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }
    
    //格式化日期 2011-03-02 18:22:34或unix时间戳 转换为：3月2日 18时22分
    function fmt_date($date, $time_flg = TRUE) {
        $dateOri = $date;
        if (!strstr($date, '-')) {
            $date = date("Y-m-d H:i:s", $date);
        }
        $date = substr($date, 5, 11);
        $date_array1 = explode('-', $date);
        $month = (int)$date_array1[0];
        $date_array2 = explode(' ', $date_array1[1]);
        $day = (int)$date_array2[0];
        
        $date2 = substr($dateOri,11);
        $dateArr2 = explode(':', $date2);
        $otherdate = $dateArr2[0].'时'.$dateArr2[1].'分';
        if ($time_flg) {
            return $month . '月' . $day . '日 ' . $otherdate;
        } else {
            return $month . '月' . $day . '日 ';
        }
    }
	/**
	 * 创建二维码
	 * @param unknown $data
	 * @param unknown $file_name
	 * @param string $level
	 * @param number $size
	 */
	function create_qrcode($data,$file_name,$level = 'M',$size = 8){//L/M/Q/H
		vendor("Phpqrcode.phpqrcode");
		$path = $_SERVER['DOCUMENT_ROOT'].C('EWM_PATH');
		if(!is_dir(C('EWM_PATH'))) mkdir($path,0777);
		QRcode::png($data,$path.$file_name, $level, $size,2);
		return C('EWM_PATH').$file_name;
	}
	
	/**
	 * 将键值为数组的值序列化以便存入数组
	 * @param unknown $data
	 * @return boolean|string
	 */
	function input_data($data){
		if (empty($data)) return false;
		if(is_array($data)){
			foreach ($data as $key=>$val){
				if(is_array($val)){
					$data[$key] = serialize($val);
				}
			}
		}
		return $data;
	}
	
	/**
	 * 反序列化数组中的序列化字段|反序列化字符串
	 * @param unknown $data
	 * @return boolean|mixed
	 */
	function output_data($data){
		if (empty($data)) return false;
		if(is_array($data)){
			foreach ($data as $key=>$val){
				if(is_serialized($val)){
					$data[$key] = unserialize($val);
				}
			}
		}else{
			is_serialized($data) && $data = unserialize($data);
		}
		return $data;
	}
	
	/**
	 * 判断字符串是不是序列化的
	 * @param unknown $data
	 * @return boolean
	 */
	function is_serialized( $data ) {
		$data = trim( $data );
		if ( 'N;' == $data ) return true;
		if ( !preg_match( '/^([adObis]):/', $data, $badions ) ) return false;
		switch ( $badions[1] )
		{
			case 'a' : ;
			case 'o' : ;
			case 's' :if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) ) return true;break;
			case 'b' : ;
			case 'i' : ;
			case 'd' :if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) ) return true;break;
		}
		return false;
	}
	
	/**
	 * 发送短信
	 * @param unknown $mobile 手机号
	 * @param unknown $msg 短信内容
	 */
	function sendSMS($mobile, $msg){
		vendor('ChuanglanSms.ChuanglanSmsApi');
		$smsapi = new \ChuanglanSmsApi();
		$result = $smsapi->sendSMS($mobile, $msg);
// 		var_dump($result);
	}

    //生成订单号
    function get_order_sn(){
      return date('YmdHis').mt_rand(1000,9999);    
    }
	
        //把中文转换成拼音
    function getfirstchar($s0){    
    $fchar = ord($s0{0});  
    if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});  
    $s1 = get_encoding($s0,'GB2312');  
    $s2 = get_encoding($s1,'UTF-8');  
    if($s2 == $s0){$s = $s1;}else{$s = $s0;}  
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;  
    if($asc >= -20319 and $asc <= -20284) return "A";  
    if($asc >= -20283 and $asc <= -19776) return "B";  
    if($asc >= -19775 and $asc <= -19219) return "C";  
    if($asc >= -19218 and $asc <= -18711) return "D";  
    if($asc >= -18710 and $asc <= -18527) return "E";  
    if($asc >= -18526 and $asc <= -18240) return "F";  
    if($asc >= -18239 and $asc <= -17923) return "G";  
    if($asc >= -17922 and $asc <= -17418) return "I";  
    if($asc >= -17417 and $asc <= -16475) return "J";  
    if($asc >= -16474 and $asc <= -16213) return "K";  
    if($asc >= -16212 and $asc <= -15641) return "L";  
    if($asc >= -15640 and $asc <= -15166) return "M";  
    if($asc >= -15165 and $asc <= -14923) return "N";  
    if($asc >= -14922 and $asc <= -14915) return "O";  
    if($asc >= -14914 and $asc <= -14631) return "P";  
    if($asc >= -14630 and $asc <= -14150) return "Q";  
    if($asc >= -14149 and $asc <= -14091) return "R";  
    if($asc >= -14090 and $asc <= -13319) return "S";  
    if($asc >= -13318 and $asc <= -12839) return "T";  
    if($asc >= -12838 and $asc <= -12557) return "W";  
    if($asc >= -12556 and $asc <= -11848) return "X";  
    if($asc >= -11847 and $asc <= -11056) return "Y";  
    if($asc >= -11055 and $asc <= -10247) return "Z";  
    return null;  
}  
/** 
 * @name: get_encoding 
 * @description: 自动检测内容编码进行转换 
 * @param: string data 
 * @param: string to  目标编码 
 * @return: string 
**/  
function get_encoding($data,$to){  
    $encode_arr=array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP');   
    $encoded=mb_detect_encoding($data, $encode_arr);   
    $data = mb_convert_encoding($data,$to,$encoded);   
    return $data;  
}   
   
function pinyin1($zh){  
    $ret = "";  
    $s1 = iconv("UTF-8","gb2312", $zh);  
    $s2 = iconv("gb2312","UTF-8", $s1);  
    if($s2 == $zh){$zh = $s1;}  
    for($i = 0; $i < strlen($zh); $i++){  
        $s1 = substr($zh,$i,1);  
        $p = ord($s1);  
        if($p > 160){  
            $s2 = substr($zh,$i++,2);  
            $ret .= getfirstchar($s2);  
        }else{  
            $ret .= $s1;  
        }  
    }  
    return $ret;  
}  